<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p="pilferpage" xmlns="http://www.w3.org/1999/XSL/Format">

	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>

	<!-- Editable variables-->
	<xsl:variable name="pageWidthMillimetres" select="297"/>
	<xsl:variable name="pageHeightMillimetres" select="210"/>

	<xsl:variable name="topHeightMillimetres" select="15"/>
	<xsl:variable name="rightWidthMillimetres" select="25"/>
	<xsl:variable name="bottomHeightMillimetres" select="25"/>
	<xsl:variable name="leftWidthMillimetres" select="15"/>

	<xsl:variable name="pagePaddingTopMillimetres" select="10"/>
	<xsl:variable name="pagePaddingRightMillimetres" select="10"/>
	<xsl:variable name="pagePaddingBottomMillimetres" select="10"/>
	<xsl:variable name="pagePaddingLeftMillimetres" select="10"/>

	<!-- Non-editable below -->

	<xsl:variable name="bodyWidth" select=" $pageWidthMillimetres - $leftWidthMillimetres - $rightWidthMillimetres - $pagePaddingLeftMillimetres - $pagePaddingRightMillimetres"/>
	
	<xsl:template match="/p:page">
		<root>
			<layout-master-set>
				<simple-page-master master-name="master-sequence" margin="{$pagePaddingTopMillimetres}mm {$pagePaddingRightMillimetres}mm {$pagePaddingBottomMillimetres}mm {$pagePaddingLeftMillimetres}mm" page-height="{$pageWidthMillimetres}mm" page-width="{$pageHeightMillimetres}mm">
					<region-body margin="{$topHeightMillimetres}mm {$rightWidthMillimetres}mm {$bottomHeightMillimetres}mm {$leftWidthMillimetres}mm"/>
		      			<region-before extent="{$topHeightMillimetres}mm" display-align="after"/>
					<region-after extent="{$bottomHeightMillimetres}mm" display-align="before"/>
					<region-start extent="{$leftWidthMillimetres}mm"/>
					<region-end extent="{$rightWidthMillimetres}mm"/>
				</simple-page-master>
			</layout-master-set>
			<page-sequence master-reference="master-sequence">
				<static-content flow-name="xsl-footnote-separator">
					<block/>
				</static-content>
				<static-content flow-name="xsl-region-before">
					<block/>
			  </static-content>
			  <static-content flow-name="xsl-region-start">
			   	<block/>
				</static-content>
				<static-content flow-name="xsl-region-end">
					<block/>
				</static-content>
				<static-content flow-name="xsl-region-after">
          <block/>
				</static-content>
				<flow flow-name="xsl-region-body">
					<xsl:apply-templates> 
						<xsl:with-param name="availableWidthInMillimetres" select="$pageWidthMillimetres - $rightWidthMillimetres - $leftWidthMillimetres"/>
					</xsl:apply-templates>
				</flow>
			</page-sequence>
		</root>
	</xsl:template>

	<xsl:template match="p:table">
		<xsl:param name="availableWidthInMillimetres"/>

		<xsl:apply-templates select="p:title"/>
		<table table-layout="fixed" width="100%">
			<xsl:variable name="numberOfColumns">
				<xsl:choose>
					<xsl:when test="p:head/p:row[1]/p:cell"><xsl:value-of select="count(p:head[1]/p:row[1]/p:cell)"/></xsl:when>
					<xsl:when test="p:body/p:row[1]/p:cell"><xsl:value-of select="count(p:body[1]/p:row[1]/p:cell)"/></xsl:when>
					<xsl:when test="p:row[1]/p:cell"><xsl:value-of select="count(p:row[1]/p:cell)"/></xsl:when>
				</xsl:choose>
			</xsl:variable>
			<xsl:for-each select="descendant::*[position() &lt;= $numberOfColumns]">
				<table-column column-width="{$availableWidthInMillimetres div $numberOfColumns}mm"/>
			</xsl:for-each>
			<xsl:if test="p:head">
				<table-head>
					<xsl:apply-templates select="p:head"/>
				</table-head>
			</xsl:if>
			<table-body>
				<xsl:apply-templates select="p:row | p:body/p:row"/>
			</table-body>
			<xsl:if test="p:foot">
				<table-foot>
					<xsl:apply-templates select="p:foot"/>
				</table-foot>
			</xsl:if>
		</table>
	</xsl:template>

	<xsl:template match="p:row">
		<table-row>
			<xsl:apply-templates select="p:cell"/>
		</table-row>
		<xsl:apply-templates select="p:row"/>
	</xsl:template>

	<xsl:template match="p:cell">
		<xsl:variable name="ancestorRows" select="count(ancestor::p:row)"/>
		<table-cell text-align="left" vertical-align="top">
			<xsl:if test="not(preceding-sibling::p:cell) and $ancestorRows &gt; 1">
				<xsl:attribute name="padding-left"><xsl:value-of select="$ancestorRows - 1"/>em</xsl:attribute>
			</xsl:if>
			<xsl:if test="ancestor::p:head or ancestor::p:foot">
				<xsl:attribute name="background">#eeeeee</xsl:attribute>
			</xsl:if>
			<block>
				<xsl:apply-templates/>
			</block>
		</table-cell>
	</xsl:template>


	<xsl:template match="p:paragraph">
		<xsl:element name="block">
      <xsl:if test="@margin-top">
        <xsl:attribute name="margin-top"><xsl:value-of select="@margin-top"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="@margin-bottom">
        <xsl:attribute name="margin-bottom"><xsl:value-of select="@margin-bottom"/></xsl:attribute>
      </xsl:if>

      <xsl:apply-templates/>
    </xsl:element>
	</xsl:template>

	<xsl:template match="p:link">
		<xsl:element name="basic-link">
			<xsl:attribute name="color">blue</xsl:attribute>
			<xsl:attribute name="text-decoration">underline</xsl:attribute>
			<xsl:attribute name="external-destination">url('<xsl:value-of select="@url"/>')</xsl:attribute>
			<xsl:attribute name="color">blue</xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:title">
		<xsl:variable name="ancestorSections" select="ancestor::p:section"/>
		<xsl:element name="block">
      <xsl:attribute name="margin-bottom">1cm</xsl:attribute>
			<xsl:attribute name="font-size">
				<xsl:choose>
					<xsl:when test="count($ancestorSections) &gt; 4">
						<xsl:text>x-small</xsl:text>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 3">
						<xsl:text>small</xsl:text>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 2">
						<xsl:text>medium</xsl:text>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 1">
						<xsl:text>large</xsl:text>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 0">
						<xsl:text>x-large</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>xx-large</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>	


</xsl:stylesheet>
